import { Component, DoCheck, ElementRef, EventEmitter, Input, Output, Renderer2 } from '@angular/core';
import { CommonService } from '../common.service';
import { CommonDialog, Dialog } from '../model/commom.model';

@Component({
  selector: 'app-dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.scss']
})

/**
 * 描述：弹框公共组件TS
 * 公司：xxx有限公司
 * 版权：xxx2020
 * 作者：oyjl
 * 时间：2020-04-27 14:15:03
 */
export class DialogComponent implements DoCheck {

  @Input() commonDialog: CommonDialog;
  @Input() showDialog: boolean; // 显示弹框
  @Output() showDialogChange: EventEmitter<boolean> = new EventEmitter();
  public dialog: Dialog;
  public reason: string;
  public dialogStatus: boolean;
  constructor(
    public elementRef: ElementRef,
    public renderer: Renderer2,
    public commonService: CommonService) { }

  ngDoCheck(): void {
    if (this.commonDialog) {
      this.dialog = this.commonDialog.dialog;
    }
  }

  /**
   * 描述：确定按钮保存控制
   * 作者：oyjl
   * 时间：2020-04-27 14:39:54
   */
  public saveHandle() {
    this.dialog.callback(this.reason, this);
  }

  /**
   * 描述：关闭弹框
   * 作者：oyjl
   * 时间：2020-04-27 16:28:38
   */
  public closeDialog() {
    this.reason = '';
    this.showDialog = false;
    this.showDialogChange.emit(false);
  }

  /**
   * 描述：弹框头部变量字段解析
   * 作者：oyjl
   * 时间：2020-04-29 10:25:16
   */
  public headFielResolve(head) {
    if (this.commonService.cacheData) {
      const fields = head.match(/(\${)(.*?)(\})/g);
      fields.forEach(element => {
        const field = element.replace(/(\${)(.*?)(\})/g, '$2');
        const text = this.commonService.cacheData[field];
        head = head.replace(`\$\{${field}\}`, text);
      });
    }
    return head;
  }
}
